//
//  HDHTabBar.m
//  01- HDH彩票(环境部署)
//
//  Created by hdh1997 on 15/10/26.
//  Copyright © 2015年 hdh1997. All rights reserved.
//

#import "HDHTabBar.h"
#import "HDHTabBarButton.h"

@interface HDHTabBar ()

@property (nonatomic ,weak) UIButton *selectdeBtn;

@end

@implementation HDHTabBar


//-(NSMutableArray *)buttons
//{
//    if (_buttons == nil) {
//        _buttons = [NSMutableArray array];
//    }
//    return _buttons;
//}

-(void)setItems:(NSArray *)items
{
    _items = items;
    
    for (int i = 0; i < items.count; i++) {
        
        //1.取出对应的按钮模型
        UITabBarItem *item = items[i];
        HDHTabBarButton *btn = [HDHTabBarButton buttonWithType:UIButtonTypeCustom];
        btn.tag = i;
        //根据对应的模型,设置按钮的图片
//        [btn setBackgroundImage:item.image forState:UIControlStateNormal];
//        [btn setBackgroundImage:item.selectedImage forState:UIControlStateSelected];
        [btn setImage:item.image forState:UIControlStateNormal];
//        btn.imageEdgeInsets = UIEdgeInsetsMake(0, 5, 0, -5);
//        ;
//        [btn setTitle:@"nnn" forState:UIControlStateNormal];
        [btn setImage:item.selectedImage forState:UIControlStateSelected];
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchDown];
        
        if (i == 0) {
            [self btnClick:btn];
        }
        [self addSubview:btn];
    }


}

//在tabBarBtn上添加label


//点击tabBar上的按钮就会调用
-(void)btnClick:(UIButton *)btn
{
    _selectdeBtn.selected = NO;
    btn.selected = YES;
    _selectdeBtn = btn;
    
    //通知代理tabBarVc切换控制器
    if ([_delegate respondsToSelector:@selector(tabBar:didClickBtn:)]) {
        [_delegate tabBar:self didClickBtn:btn.tag];
    }
    

}


#pragma mark - 布局子控件的位置
-(void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat btnW = self.bounds.size.width / _items.count;
    CGFloat btnH = self.bounds.size.height;
    CGFloat btnX = 0;
    CGFloat btnY = 0;
    
//    int i = 0;
    
//    for (UIView *tabBarButton in self.buttons) {
//        
//        btnX = i * btnH;
//        if (i == 4) {
//            
//            UIButton *but = [[UIButton alloc]initWithFrame:CGRectMake(btnX, btnY - 15, btnW, btnH + 15)];
//            
//            [but setBackgroundImage:[UIImage imageNamed:@"gouwu"] forState:UIControlStateNormal];
//            [but addTarget:self action:@selector(clickShopping:) forControlEvents:UIControlEventTouchUpInside];
//            but.tag = 6;
//            [self addSubview:but];
//        }
//        
//        tabBarButton.frame = CGRectMake(btnX, btnY, btnW, btnH);
//        i++;
//    }
    for (int i = 0; i < _items.count; i++) {
        
        
        UIButton *btn = self.subviews[i];
        btnX = i * btnW;
        btn.frame = CGRectMake(btnX, btnY, btnW, btnH);
    }
    

}
//
//-(void)clickShopping:(UIButton *)but
//{
//    //通知代理tabBarVc切换控制器
//    if ([_delegate respondsToSelector:@selector(tabBar:didClickBtn:)]) {
//        [_delegate tabBar:self didClickBtn:but.tag];
//
//
//}
//    
//}


@end
